package com.shwread.android.ui.page.chat.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ciko.code.logic.bean.AddressBookInfo;
import com.mywork.WorkUIKit;
import com.shwread.android.qysw.activity.SelectApproverActivity;
import com.shwread.android.qysw.bean.BusinessEvent;
import com.shwread.android.qysw10000187.R;
import com.shwread.android.ui.dialog.BusinessDialog;
import com.shwread.android.ui.dialog.SimpleListener;
import com.shwread.android.ui.widget.CircleImageView;
import com.squareup.picasso.Picasso;

import java.util.HashMap;
import java.util.List;

import de.greenrobot.event.EventBus;
import logic.bean.GroupBean;
import logic.util.Utils;
import logic.util.logutil.LogUtil;

/**
 * 选择审批人adapter
 */
public class SelectApproverAdapter  extends
        BaseExpandableListAdapter
{

    private final Context context;

    private List<GroupBean> mGroupData;

    private HashMap<String, List<AddressBookInfo>> mChildData;

    private LayoutInflater mInflater;
    private ExpandableListView lvContacts;

    private String isOrganiser="true";


    public SelectApproverAdapter(Context context, ExpandableListView lvContacts, List<GroupBean> mGroupData, HashMap<String, List<AddressBookInfo>> mChildData) {
        this.context = context;
        this.lvContacts = lvContacts;
        this.mChildData = mChildData;
        this.mGroupData = mGroupData;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    @Override
    public int getGroupCount() {
        return null != mGroupData ? mGroupData.size() : 0;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        if (null == mChildData || null == mGroupData || mGroupData.size() == 0 || null == mGroupData.get(groupPosition) || null == mChildData.get(mGroupData.get(groupPosition).getName()))
        {
            return 0;
        }
        else
        {
            return mChildData.get(mGroupData.get(groupPosition).getName()).size();
        }
    }

    @Override
    public Object getGroup(int groupPosition) {
        return mGroupData.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        if (null == mChildData || null == mGroupData || mGroupData.size() == 0 || null == mGroupData.get(groupPosition) || null == mChildData.get(mGroupData.get(groupPosition).getName()))
        {
            return null;
        }
        else
        {
            return mChildData.get(mGroupData.get(groupPosition).getName()).get(childPosition);
        }
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        View v;
        if (convertView == null)
        {
            v = mInflater.inflate(isExpanded ? R.layout.chat_group_item : R.layout.chat_group_item, parent, false);
        }
        else
        {
            v = convertView;
        }

        final GroupBean group = mGroupData.get(groupPosition);
        TextView groupNameTV = (TextView) v.findViewById(R.id.item_expandable_group);
        final ImageView openIV = (ImageView) v.findViewById(R.id.item_img_open);
        LinearLayout llGroup = (LinearLayout) v.findViewById(R.id.ll_item_group);
        groupNameTV.setText(group.getName());
        if (SelectApproverActivity.isClose[groupPosition] == 0)
        {
            openIV.setImageResource(R.drawable.change_store_icon);
        }
        else
        {
            openIV.setImageResource(R.drawable.change_store_icon_up);
        }
        llGroup.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) {
                if (SelectApproverActivity.isClose[groupPosition] != 0)
                {
                    lvContacts.collapseGroup(groupPosition);
                    openIV.setImageResource(R.drawable.change_store_icon);
                    SelectApproverActivity.isClose[groupPosition] = 0;
                }
                else
                {
                    for (int i = 0; i < mGroupData.size(); i++)
                    {
                        if (SelectApproverActivity.isClose[i] == 1)
                        {
                            lvContacts.collapseGroup(i);
                            SelectApproverActivity.isClose[i] = 0;
                        }
                    }
                    lvContacts.expandGroup(groupPosition);
                    openIV.setImageResource(R.drawable.change_store_icon_up);
                    SelectApproverActivity.isClose[groupPosition] = 1;
                }
            }
        });
        return v;
    }

    @Override
    public View getChildView(final int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        View view;
        if (convertView == null)
        {
            view = mInflater.inflate(isLastChild ? R.layout.select_approver : R.layout.select_approver, parent, false);
        }
        else
        {
            view = convertView;
        }
        final AddressBookInfo contactItem = (AddressBookInfo) getChild(groupPosition, childPosition);
        CircleImageView contactsHeadImg = (CircleImageView) view.findViewById(R.id.iv_chat_head_img);
        ImageView contactsHeader = (ImageView) view.findViewById(R.id.iv_chat_contact_icon_header);
        ImageView contactsCall = (ImageView) view.findViewById(R.id.iv_chat_contact_icon_call);
        ImageView contactsMsg = (ImageView) view.findViewById(R.id.iv_chat_contact_icon_msg);
        ImageView contactsChat = (ImageView) view.findViewById(R.id.iv_chat_contact_icon_chat);
        contactsChat.setTag(childPosition);
        final TextView contactsName = (TextView) view.findViewById(R.id.tv_chat_contact_name);
        RelativeLayout rlItem = (RelativeLayout) view.findViewById(R.id.rl_contact_item);
        if (Utils.isNotEmpty(contactItem.getUserHead()))
        {
            Picasso.with(context).load(contactItem.getUserHead()).into(contactsHeadImg);
        }
        else
        {
            Picasso.with(context).load(R.drawable.chat_head_img).into(contactsHeadImg);
        }
        if (contactItem.getGroupPosition() == 1)
        {
            Picasso.with(context).load(R.drawable.chat_contract_icon_header).into(contactsHeader);
            contactsHeader.setVisibility(View.VISIBLE);
        }
        else if (contactItem.getGroupPosition() == 2)
        {
            Picasso.with(context).load(R.drawable.chat_contract_icon_deputy_header).into(contactsHeader);
            contactsHeader.setVisibility(View.VISIBLE);
        }
        else
        {
            contactsHeader.setVisibility(View.GONE);
        }
        contactsName.setText(contactItem.getContactName());
        if (Utils.isEmpty(contactItem.getMobileNumber()))
        {
            contactsMsg.setVisibility(View.GONE);
            contactsChat.setVisibility(View.GONE);
        }
        else
        {
//            contactsMsg.setVisibility(View.VISIBLE);
//            contactsChat.setVisibility(View.VISIBLE);
        }

       rlItem.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) {


//                isOrganiser = ((SelectApproverActivity) context).getIntent().getStringExtra("isOrganiser");

                if("false".equals(((SelectApproverActivity) context).getIntent().getStringExtra("isOrganiser"))) {
                    if (WorkUIKit.getUserId().equals(contactItem.getUserId())){
                        Log.e("fff","fffff");
                        Toast.makeText(context,"不能选自己为审批人，请重新选择",Toast.LENGTH_SHORT).show();
                    }else {
                        Intent intent = new Intent();
                        intent.putExtra("Approver", contactItem);
                        ((SelectApproverActivity) context).setResult(1011, intent);
                        ((SelectApproverActivity) context).finish();
                    }

                }else {
                    BusinessDialog businessDialog =new BusinessDialog(context,new SimpleListener() {
                        @Override
                        public void ok() {

                            //EventBus发送数据
                            EventBus.getDefault().post(new BusinessEvent(Integer.parseInt(contactItem.getUserId()),contactItem.getContactName()));
                        ((SelectApproverActivity) context).finish();

                        }
                    });
                    businessDialog.show();
                }


            }
        });

        return view;
    }


    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    @Override
    public boolean isEmpty() {
        return mGroupData != null ? mGroupData.isEmpty() : true;
    }

    public final void load(boolean reload) {
        if (!reload && !isEmpty()) { return; }

        LogUtil.d("fw", "load");
    }



}
